package com.techsoul.module.accounting.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ExcelException;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.core.util.poi.ExcelExport;
import com.techsoul.common.core.util.poi.ExcelImport;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.accounting.enmus.DiscountActivityTypeEnum;
import com.techsoul.module.accounting.mapper.PromotionDiscountPriceMapper;
import com.techsoul.module.accounting.pojo.entity.PromotionDiscountPrice;
import com.techsoul.module.accounting.pojo.param.PromotionDiscountPriceSaveParam;
import com.techsoul.module.accounting.pojo.param.PromotionDiscountPriceSearchParam;
import com.techsoul.module.accounting.pojo.vo.PromotionDiscountPriceVo;
import com.techsoul.module.accounting.service.IPromotionDiscountPriceService;
import com.techsoul.module.basic.pojo.vo.StoreVo;
import com.techsoul.module.basic.service.IStoreService;
import com.techsoul.module.goods.enmus.SpuStatusEnum;
import com.techsoul.module.goods.mapper.SpuMapper;
import com.techsoul.module.goods.pojo.entity.Spu;
import com.techsoul.module.marketing.mapper.CouponActivityMapper;
import com.techsoul.module.marketing.pojo.vo.CouponActivityVo;
import com.techsoul.module.pms.mapper.CustGoodsMapMapper;
import com.techsoul.module.pms.mapper.GoodsGroupMapper;
import com.techsoul.module.pms.mapper.GoodsSupplierMapper;
import com.techsoul.module.goods.mapper.SkuMapper;
import com.techsoul.module.pms.pojo.entity.CustGoodsMap;
import com.techsoul.module.pms.pojo.entity.GoodsGroup;
import com.techsoul.module.pms.pojo.entity.GoodsSupplier;
import com.techsoul.module.goods.pojo.entity.Sku;
import com.techsoul.module.pms.pojo.param.CustGoodsMapSaveParam;
import com.techsoul.module.pms.pojo.vo.CustGoodsMapVo;
import com.techsoul.module.pms.pojo.vo.GoodsSupplierVo;
import com.techsoul.module.system.enmus.ErrorType;
import com.techsoul.module.system.mapper.OperationLogMapper;
import com.techsoul.module.system.pojo.entity.OperationLog;
import com.techsoul.module.system.service.IConfigService;
import com.techsoul.module.system.service.IGeneralSeqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 促销活动的采购结算价格配置 Service实现
 *
 * @author liuen
 * @date 2021-04-11
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class PromotionDiscountPriceServiceImpl implements IPromotionDiscountPriceService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private PromotionDiscountPriceMapper mapper;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private IGeneralSeqService generalSeqService;
    @Autowired
    private IConfigService configService;

    @Autowired
    private CustGoodsMapMapper custGoodsMapMapper;

    @Autowired
    private IStoreService iStoreService;

    @Autowired
    private CouponActivityMapper couponActivityMapper;

    @Autowired
    private GoodsGroupMapper goodsGroupMapper;
    @Autowired
    private OperationLogMapper operationLogMapper;

    @Autowired
    private GoodsSupplierMapper goodsSupplierMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuMapper skuMapper;
    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(PromotionDiscountPriceSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        PromotionDiscountPrice entity = new PromotionDiscountPrice();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(PromotionDiscountPriceSaveParam params) {
        PromotionDiscountPrice entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public PromotionDiscountPriceVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<PromotionDiscountPriceVo> findAllList(PromotionDiscountPriceSearchParam params) {
        return this.mapper.findAllList(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<PromotionDiscountPriceVo> findAllList(PromotionDiscountPriceSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAllList(new Page<>(current, pageSize), params);
    }
    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<PromotionDiscountPriceVo> findAll(PromotionDiscountPriceSearchParam params) {
        if(!StringUtils.isNull(params.getActType())){
            if(params.getActType().equals("!")){
                params.setActType("领券");
                return this.mapper.findAllList(params);
            }else{
                return this.mapper.findAll(params);
            }
        }else{
            return this.mapper.findAll(params);
        }

    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<PromotionDiscountPriceVo> findAll(PromotionDiscountPriceSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }
    /**
     * 导入模板
     */
    @Override
    public String importTemplate() {
        String baseDir = configService.findByConfigKey("project.attachment.baseDir");
        String baseUrl = configService.findByConfigKey("project.attachment.baseUrl");
        String fileName = "导入模板-供应商-优惠活动商品信息和价格.xlsx";
        try {
            new ExcelExport(null, PromotionDiscountPriceVo.class).writeFile(baseDir + fileName).close();
        } catch (Exception e) {
            log.error("生成优惠活动商品信息和价格导入模板失败", e);
            throw new ExcelException(e.getMessage());
        }
        return baseUrl + fileName;
    }

    /**
     * 页面输入
     * @param saveParam
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insertData(PromotionDiscountPriceSaveParam saveParam) {

        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String successMsg = "success";
        StringBuilder failureMsg = new StringBuilder();
        StoreVo storeVo = iStoreService.findById(saveParam.getStoreId());
        String supplierNo = storeVo.getSupplierId();
        String supplierName = storeVo.getSupplierName();

        CouponActivityVo cuponActivityVo = null;
        if(StringUtils.isNull(saveParam.getActType())){
            OperationLog operationLog =new OperationLog();
            operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
            operationLog.setErrorMsg("活动ID没有提供");
            operationLog.setMethod("导入模板-供应商-优惠活动价格");
            operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
            if (user != null) {
                operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
            }
            operationLogMapper.insert(operationLog);

            return "新建失败，活动ID没有提供";
        }else{
            cuponActivityVo =couponActivityMapper.findById(saveParam.getActType());
            if(saveParam.getApplyDate()==null) {
                if (cuponActivityVo.getStartTime() == null) {

                    OperationLog operationLog = new OperationLog();
                    operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                    operationLog.setErrorMsg("活动开始时间没有提供");
                    operationLog.setMethod("导入模板-供应商-优惠活动价格");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);

                    return "新建失败，活动开始时间没有提供";
                }
            }
            if(saveParam.getEffectUntilDate()==null) {
                if (cuponActivityVo.getEndTime() == null) {
                    OperationLog operationLog = new OperationLog();
                    operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                    operationLog.setErrorMsg("活动结束时间没有提供");
                    operationLog.setMethod("导入模板-供应商-优惠活动价格");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);

                    return "新建失败，活动结束时间没有提供";
                }
            }
        }
        Spu newspu = new Spu();
        List<String> scaSpuNos = new ArrayList<String>();

        saveParam = this.removeSpecialCharPromotionDiscountPriceVo(saveParam);

            String custGoodNo = saveParam.getCsaSpuCode();
            String addSpuYn="Y";
            if(StringUtils.isNull(saveParam.getCustRelSalesPrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("SPUID".concat(custGoodNo).concat("相应的销售价格没有提供"));

                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("SPU:".concat(saveParam.getCsaSpuCode()));
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);//add log


            }else{
                if(!StringUtils.isDouble(saveParam.getCustRelSalesPrice())){
                    //add log
                    OperationLog operationLog =new OperationLog();
                    operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                    operationLog.setErrorMsg("SPUID".concat(custGoodNo).concat("相应得销售价格格式不对"));
                    operationLog.setRequestMethod(custGoodNo);
                    operationLog.setLocation("SPU:".concat(saveParam.getCsaSpuCode()));
                    operationLog.setMethod("导入模板-供应商-优惠活动价格");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);//add log

                }
            }
            if(StringUtils.isNull(saveParam.getSettlePrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("采购常规价格没有提供");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("SPU:".concat(saveParam.getCsaSpuCode()));
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);

            }


            if(scaSpuNos.contains(custGoodNo)){
                List<Spu> spus =  this.spuMapper.findByCustSpuNo(custGoodNo,storeVo.getMallId());
                if(spus!=null&&spus.size()>0){
                    newspu =(Spu)spus.get(0);
                    addSpuYn = "N";
                }

            }else{
                scaSpuNos.add(custGoodNo);
            }

              if(saveParam.getEffectUntilDate()==null){
                  saveParam.setEffectUntilDate(cuponActivityVo.getEndTime());
                }
                if(saveParam.getApplyDate()==null){
                    saveParam.setApplyDate(cuponActivityVo.getStartTime());
                }
        saveParam.setActivityCode(cuponActivityVo.getActivityCode());
        saveParam.setActType(cuponActivityVo.getActivityType());

                if(StringUtils.isNull(saveParam.getCsaSkuCode())) { // 导入数据没有SKU
                    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo, "通用", storeVo.getMallId());
                    if(cgm==null) {  //映射表内没有
                     /*   CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());

                        custGoodsMapSaveParam.setGoodsType("spu");
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(saveParam);
                        }


                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        //  custGoodsMapSaveParam.setSkuId(newsku.getId());
                        //  String cgmid =iCustGoodsMapService.insert(custGoodsMapSaveParam);


                        saveParam.setGroupOrSpu("spu");
                        saveParam.setCsaSkuCode("通用");
                        saveParam.setCsaSpuCode(saveParam.getCsaSpuCode());
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());*/
                        //  }

                        return "新建失败，该客户SPU编号不在系统中，请先登记SPU";
                    }else{ // 有数据
                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,cgm.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                        }
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                            saveParam.setCustGoodsName(cgm.getSpuName());
                            saveParam.setCsaSpuCode(cgm.getCustGoodsId());


                            saveParam.setMaterialMapId(cgm.getId());

                    }
                }else{ // 导入数据有SKU
                    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo, saveParam.getCsaSkuCode(), storeVo.getMallId());
                    if(cgm==null) {  //映射表内没有\


                   /*     CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();

                        custGoodsMapSaveParam.setGoodsType("sku");
                        custGoodsMapSaveParam.setCustSkuNo(saveParam.getCsaSkuCode());
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(saveParam);
                        }
                        Sku newsku = this.generateNewSku(saveParam,newspu);
                        custGoodsMapSaveParam.setSkuId(newsku.getId());
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                       saveParam.setGroupOrSpu("sku");
                        saveParam.setCsaSkuCode(saveParam.getCsaSkuCode());
                        saveParam.setCsaSpuCode(saveParam.getCsaSpuCode());
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);

                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                     */

                        return "新建失败，该客户SKU编号不在系统中，请先登记SKU";
                    }else {  // 有映
                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,cgm.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                        }

                        saveParam.setMaterialMapId(cgm.getId());



                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(saveParam.getCsaSkuCode());
                            saveParam.setCsaSpuCode(saveParam.getCsaSpuCode());
                            saveParam.setCustGoodsName(cgm.getSkuName());


                        }// end else
                    }


                saveParam.setActivityType(DiscountActivityTypeEnum.BP.getStatusCode());
                saveParam.setStoreId(saveParam.getStoreId());
                saveParam.setMallCustNo(storeVo.getMallId());

                if(saveParam.getWholeSaleQuantity()!=null){
                    saveParam.setWholeSaleQuantity(saveParam.getWholeSaleQuantity());
                }else{
                    saveParam.setWholeSaleQuantity(new Long(0));
                }
                saveParam.setCustGoodsName(saveParam.getCustGoodsName());
                saveParam.setGroupOrSpu(saveParam.getGroupOrSpu());
                 this.insert(saveParam);

              return successMsg;
    }
    /**
     * 导入
     * @param file
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String importData(String storeId, String mallCustNo,String actType, MultipartFile file) {
        List<PromotionDiscountPriceVo> dataList = null;
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        StoreVo storeVo = iStoreService.findById(storeId);
        String supplierNo = storeVo.getSupplierId();
        String supplierName = storeVo.getSupplierName();
        int successNum = 0;
        int failureNum = 0;
        int logNum = 0;
        try {
            ExcelImport excelImport = new ExcelImport(file, 1, 0);
            dataList = excelImport.getDataList(PromotionDiscountPriceVo.class);
        } catch (Exception e) {
            log.error("获取数据信息失败!");
            throw new ExcelException("获取数据信息失败!");
        }
        if (ListUtils.isEmpty(dataList)) {
            throw new ExcelException("导入数据不能为空！");
        }
        CouponActivityVo cuponActivityVo = null;
        if(StringUtils.isNull(actType)){
            OperationLog operationLog =new OperationLog();
            operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
            operationLog.setErrorMsg("活动ID没有提供");
            operationLog.setMethod("导入模板-供应商-优惠活动价格");
            operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
            if (user != null) {
                operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
            }
            operationLogMapper.insert(operationLog);
            successMsg.append("导入失败，活动ID没有提供");
            return successMsg.toString();
        }else{
            cuponActivityVo =couponActivityMapper.findById(actType);
            if(cuponActivityVo.getStartTime()==null){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("活动开始时间没有提供");
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                successMsg.append("导入失败，活动开始时间没有提供");
                return successMsg.toString();
            }
            if(cuponActivityVo.getEndTime()==null){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("活动结束时间没有提供");
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                successMsg.append("导入失败，活动结束时间没有提供");
                return successMsg.toString();
            }
        }
        Spu newspu = new Spu();
        List<String> scaSpuNos = new ArrayList<String>();
        int line = 0;
        for (PromotionDiscountPriceVo data : dataList) {
            data = this.removeSpecialCharPromotionDiscountPriceVo(data);
            line++;
            String custGoodNo = data.getCsaSpuCode();
            String addSpuYn="Y";
            if(StringUtils.isNull(data.getCustRelSalesPrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("SPUID".concat(custGoodNo).concat("相应的销售价格没有提供"));

                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);//add log
                logNum++;
                continue;
            }else{
                if(!StringUtils.isDouble(data.getCustRelSalesPrice())){
                    //add log
                    OperationLog operationLog =new OperationLog();
                    operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                    operationLog.setErrorMsg("SPUID".concat(custGoodNo).concat("相应得销售价格格式不对"));
                    operationLog.setRequestMethod(custGoodNo);
                    operationLog.setLocation("第"+line+"行");
                    operationLog.setMethod("导入模板-供应商-优惠活动价格");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);//add log
                    logNum++;
                    continue;
                }
            }
            if(StringUtils.isNull(data.getSettlePrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg("采购常规价格没有提供");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-优惠活动价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                logNum++;
                continue;
            }


            if(scaSpuNos.contains(custGoodNo)){
               List<Spu> spus =  this.spuMapper.findByCustSpuNo(custGoodNo,storeVo.getMallId());
                if(spus!=null&&spus.size()>0){
                    newspu =(Spu)spus.get(0);
                     addSpuYn = "N";
                }

            }else{
                scaSpuNos.add(custGoodNo);
            }

            // add more data check log
            try {
                PromotionDiscountPriceSaveParam saveParam = new PromotionDiscountPriceSaveParam();

                BeanUtils.copyProperties(data, saveParam);


                if(data.getEffectUntilDate()!=null){
                    saveParam.setEffectUntilDate(data.getEffectUntilDate());
                }else{
                    saveParam.setEffectUntilDate(cuponActivityVo.getEndTime());
                }
                if(data.getApplyDate()!=null){
                    saveParam.setApplyDate(data.getApplyDate());
                }else{
                    saveParam.setApplyDate(cuponActivityVo.getStartTime());
                }
                saveParam.setActivityCode(cuponActivityVo.getActivityCode());
                saveParam.setActType(cuponActivityVo.getActivityType());
                if(StringUtils.isNull(data.getCsaSkuCode())) { // 导入数据没有SKU
                    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo, "通用", storeVo.getMallId());
                    if(cgm==null) {  //映射表内没有
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());
                       /* if(data.getGroupOrSpu().equals("组合")){
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data,supplierNo,supplierName,newGoodsGroup.getId(),storeVo.getId(),storeVo.getStoreName());
                            saveParam.setCsaSkuCode("通用");
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setGroupOrSpu("gp");
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);
                        }else{*/
                        custGoodsMapSaveParam.setGoodsType("spu");
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data);
                        }


                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        //  custGoodsMapSaveParam.setSkuId(newsku.getId());
                        //  String cgmid =iCustGoodsMapService.insert(custGoodsMapSaveParam);
                        GoodsSupplier goodsSupplier =
                                this.generateNewGoodsSupplier(data,supplierNo,supplierName,newspu.getId(),storeVo.getId(),storeVo.getStoreName());


                        saveParam.setGroupOrSpu("spu");
                        saveParam.setCsaSkuCode("通用");
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                        //  }
                    }else{ // 有数据
                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,cgm.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                        }


                        if(goodsSupplierVo==null){
                            GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,cgm.getSpuId(),storeVo.getId(),storeVo.getStoreName());
                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                            saveParam.setCsaSpuCode(cgm.getCustGoodsId());

                           /* }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                                saveParam.setCsaSpuCode(cgm.getCustGoodsId());

                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                                saveParam.setCsaSpuCode(cgm.getCustGoodsId());
                            }*/
                            saveParam.setMaterialMapId(cgm.getId());
                        }else{
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                            saveParam.setCsaSpuCode(cgm.getCustGoodsId());

                           /* }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                                saveParam.setCsaSpuCode(cgm.getCustGoodsId());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());

                                saveParam.setCsaSpuCode(cgm.getCustGoodsId());
                            }*/
                            saveParam.setMaterialMapId(cgm.getId());
                        }
                    }
                }else{ // 导入数据有SKU
                    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo, data.getCsaSkuCode(), storeVo.getMallId());
                    if(cgm==null) {  //映射表内没有
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                       /* if(data.getGroupOrSpu().equals("组合")){
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setCustSkuNo(data.getCsaSkuCode());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data,supplierNo,supplierName,newGoodsGroup.getId(),storeVo.getId(),storeVo.getStoreName());

                            saveParam.setGroupOrSpu("gp");
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);

                        }else{*/
                        custGoodsMapSaveParam.setGoodsType("sku");
                        custGoodsMapSaveParam.setCustSkuNo(data.getCsaSkuCode());
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data);
                        }
                        Sku newsku = this.generateNewSku(data,newspu);
                        custGoodsMapSaveParam.setSkuId(newsku.getId());
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        this.generateNewGoodsSupplier(data,supplierNo,supplierName,newspu.getId(),storeVo.getId(),storeVo.getStoreName());
                        saveParam.setGroupOrSpu("sku");
                        saveParam.setCsaSkuCode(data.getCsaSkuCode());
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);

                        //  }
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());

                    }else {  // 有映
                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,cgm.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                        }

                        saveParam.setMaterialMapId(cgm.getId());
                        if(goodsSupplierVo==null){
                            GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,cgm.getSpuId(),storeVo.getId(),storeVo.getStoreName());
                            // if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                          /*  }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            }*/
                        }else{
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());

                           /* }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());

                            }*/

                        }// end else
                    }
                }

                saveParam.setActivityType(DiscountActivityTypeEnum.BP.getStatusCode());
                saveParam.setStoreId(storeId);
                saveParam.setMallCustNo(storeVo.getMallId());

                if(data.getWholeSaleQuantity()!=null){
                    saveParam.setWholeSaleQuantity(data.getWholeSaleQuantity());
                }else{
                    saveParam.setWholeSaleQuantity(new Long(0));
                }
                saveParam.setCustGoodsName(data.getCustGoodsName());
                saveParam.setGroupOrSpu(data.getGroupOrSpu());
                saveParam.setSettlePrice(new BigDecimal(Double.valueOf(data.getSettlePrice())));
                this.insert(saveParam);

                successNum++;
                successMsg.append("<br/>" + successNum + "导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "导入失败：";
                failureMsg.append(msg + e.getMessage());
                //add log table
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
                operationLog.setErrorMsg(failureMsg.toString());
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-商品信息和价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            OperationLog operationLog =new OperationLog();
            operationLog.setOperationType(ErrorType.PROMOTIONIMPORT.getStatusName());
            operationLog.setErrorMsg(failureMsg.toString());
            operationLog.setLocation("第"+line+"行");
            operationLog.setMethod("导入模板-供应商-商品信息和价格");
            operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
            if (user != null) {
                operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
            }
            operationLogMapper.insert(operationLog);
            throw new ExcelException(failureMsg.toString());
        } else {
            if(logNum>0){
                successMsg.insert(0, "恭喜您，操作成功！共 " + successNum + " 条，另有：" +logNum+ " 条数据不能正常导入，已写入日志");

            }else{
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

            }
        }
        return successMsg.toString();
    }
    @Override
    public PromotionDiscountPriceSaveParam removeSpecialCharPromotionDiscountPriceVo(PromotionDiscountPriceSaveParam input){

        Pattern p =null;
        Matcher m = null;;
        String rt = "";
        if(StringUtils.isNull(input.getGroupOrSpu())){
            input.setGroupOrSpu("单品");
        }else{
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getGroupOrSpu());
            rt = m.replaceAll("");
            input.setGroupOrSpu(rt);
        }
        if(!StringUtils.isNull(input.getCsaSpuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSpuCode());
            rt = m.replaceAll("");
            input.setCsaSpuCode(rt);
        }

        if(!StringUtils.isNull(input.getCsaSkuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSkuCode());
            rt = m.replaceAll("");
            input.setCsaSkuCode(rt);
        }
        if(!StringUtils.isNull(input.getCustGoodsName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsName());
            rt = m.replaceAll("");
            input.setCustGoodsName(rt);
        }



        if(!StringUtils.isNull(input.getCustRelSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustRelSalesPrice());
            rt = m.replaceAll("");
            input.setCustRelSalesPrice(rt);
        }
        if(!StringUtils.isNull(input.getCsaSkuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSkuCode());
            rt = m.replaceAll("");
            input.setCsaSkuCode(rt);
        }
        if(input.getSettlePrice()!=null) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSettlePrice().toString());
            rt = m.replaceAll("");
            if(StringUtils.isDouble(rt)){
                input.setSettlePrice(new BigDecimal(rt));
            }else{
                rt = rt.substring(1);
                input.setSettlePrice(new BigDecimal(rt));
            }

        }

        return input;
    }
    @Override
    public PromotionDiscountPriceVo removeSpecialCharPromotionDiscountPriceVo(PromotionDiscountPriceVo input){

        Pattern p =null;
        Matcher m = null;;
        String rt = "";
        if(StringUtils.isNull(input.getGroupOrSpu())){
            input.setGroupOrSpu("单品");
        }else{
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getGroupOrSpu());
            rt = m.replaceAll("");
            input.setGroupOrSpu(rt);
        }
        if(!StringUtils.isNull(input.getCsaSpuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSpuCode());
            rt = m.replaceAll("");
            input.setCsaSpuCode(rt);
        }
        if(!StringUtils.isNull(input.getCustGoodsNo())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsNo());
            rt = m.replaceAll("");
            input.setCustGoodsNo(rt);
        }

        if(!StringUtils.isNull(input.getCustGoodsName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsName());
            rt = m.replaceAll("");
            input.setCustGoodsName(rt);
        }

        if(!StringUtils.isNull(input.getCustMileSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustMileSalesPrice());
            rt = m.replaceAll("");
            input.setCustMileSalesPrice(rt);
        }

        if(!StringUtils.isNull(input.getCustRelSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustRelSalesPrice());
            rt = m.replaceAll("");
            input.setCustRelSalesPrice(rt);
        }
        if(!StringUtils.isNull(input.getCsaSkuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSkuCode());
            rt = m.replaceAll("");
            input.setCsaSkuCode(rt);
        }
        if(!StringUtils.isNull(input.getSettlePrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSettlePrice());
            rt = m.replaceAll("");
            input.setSettlePrice(rt);
        }

        return input;
    }



/**
 * 生成商品SKU
 */
    @Override
    public Sku generateNewSku(PromotionDiscountPriceSaveParam materialBuyPriceVo,Spu spu){
        Sku sku = new Sku();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        sku.setBrandId(materialBuyPriceVo.getBrandId());
        sku.setBrandName(materialBuyPriceVo.getBrandName());
        sku.setCategoryId(materialBuyPriceVo.getCategoryId());
        sku.setSpuId(spu.getId());
        sku.setCustSkuId(materialBuyPriceVo.getCsaSkuCode());
        sku.setCategoryName(materialBuyPriceVo.getCategoryName());

        sku.setSpec(materialBuyPriceVo.getMaterialGuige());
        sku.setSkuName(materialBuyPriceVo.getCustGoodsName());
        sku.setStatus(SpuStatusEnum.CREATED.getStatus());
        String skuid =   this.generalSeqService.generateSeq("sku");
        sku.setId(skuid);
        sku.preInsert(user.getId());

        skuMapper.insert(sku);
        return sku;
    }
    /**
     * 生成商品
     */
    @Override
    public Spu generateNewSpu(PromotionDiscountPriceSaveParam promotionDiscountPriceVo){
        Spu spu = new Spu();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setBrandId(promotionDiscountPriceVo.getBrandId());
        spu.setBrandName(promotionDiscountPriceVo.getBrandName());
        spu.setCategoryId(promotionDiscountPriceVo.getCategoryId());
        spu.setTitle(promotionDiscountPriceVo.getCustGoodsName());
        spu.setCategoryName(promotionDiscountPriceVo.getCategoryName());
        spu.setCustSpuNo(promotionDiscountPriceVo.getCsaSpuCode());
        spu.setIntroduction(promotionDiscountPriceVo.getCustGoodsName());
        spu.setSpuName(promotionDiscountPriceVo.getCustGoodsName());
        spu.setStatus(SpuStatusEnum.CREATED.getStatus());
        spu.setSpecs(promotionDiscountPriceVo.getMaterialGuige());
        String spuid =   this.generalSeqService.generateSeq("spu");
        spu.setId(spuid);
        spu.preInsert(user.getId());

        spuMapper.insert(spu);
        return spu;
    }
    /**
     * 生成商品SKU
     */
    @Override
    public Sku generateNewSku(PromotionDiscountPriceVo materialBuyPriceVo,Spu spu){
        Sku sku = new Sku();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        sku.setBrandId(materialBuyPriceVo.getBrandId());
        sku.setBrandName(materialBuyPriceVo.getBrandName());
        sku.setCategoryId(materialBuyPriceVo.getCategoryId());
        sku.setSpuId(spu.getId());
        sku.setCustSkuId(materialBuyPriceVo.getCustGoodsNo());
        sku.setCategoryName(materialBuyPriceVo.getCategoryName());
        sku.setImage(materialBuyPriceVo.getImage());
        sku.setSpec(materialBuyPriceVo.getMaterialGuige());
        sku.setSkuName(materialBuyPriceVo.getCustGoodsName());
        sku.setStatus(SpuStatusEnum.CREATED.getStatus());
        String skuid =   this.generalSeqService.generateSeq("sku");
        sku.setId(skuid);
        sku.preInsert(user.getId());

        skuMapper.insert(sku);
        return sku;
    }
    /**
     * 生成商品
     */
    @Override
    public Spu generateNewSpu(PromotionDiscountPriceVo promotionDiscountPriceVo){
        Spu spu = new Spu();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setBrandId(promotionDiscountPriceVo.getBrandId());
        spu.setBrandName(promotionDiscountPriceVo.getBrandName());
        spu.setCategoryId(promotionDiscountPriceVo.getCategoryId());
        spu.setTitle(promotionDiscountPriceVo.getCustGoodsName());
        spu.setCategoryName(promotionDiscountPriceVo.getCategoryName());
        spu.setImage(promotionDiscountPriceVo.getImage());
        spu.setIntroduction(promotionDiscountPriceVo.getCustGoodsName());
        spu.setSpuName(promotionDiscountPriceVo.getCustGoodsName());
        spu.setStatus(SpuStatusEnum.CREATED.getStatus());
        spu.setSpecs(promotionDiscountPriceVo.getMaterialGuige());
        String spuid =   this.generalSeqService.generateSeq("spu");
        spu.setId(spuid);
        spu.preInsert(user.getId());

        spuMapper.insert(spu);
        return spu;
    }
    /**
     * 生成商品组合
     */
    @Override
    public GoodsGroup generateNewGoodsGroup(PromotionDiscountPriceVo materialBuyPriceVo){
        GoodsGroup spu = new GoodsGroup();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setGoodsGroupName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuId(materialBuyPriceVo.getSpuId());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsGroupMapper.insert(spu);
        return spu;
    }
    /**
     * 生成供应商商品链表
     */
    @Override
    public GoodsSupplier generateNewGoodsSupplier(PromotionDiscountPriceVo materialBuyPriceVo,String supplierNo,String supplierName,String spuid,String storeId,String storeName){
        GoodsSupplier spu = new GoodsSupplier();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setSupplierId(supplierNo);
        if(materialBuyPriceVo.getGroupOrSpu().equals("单品")){
            spu.setGoodsType("spu");
        }else if(materialBuyPriceVo.getGroupOrSpu().equals("组合")){
            spu.setGoodsType("gp");
        }else{
            spu.setGoodsType("sku");
        }
        spu.setSkuName(materialBuyPriceVo.getCustGoodsName());
        spu.setSn(materialBuyPriceVo.getCustGoodsNo());
        spu.setStoreId(storeId);
        spu.setStoreName(storeName);
        spu.setSpuId(spuid);
        spu.setSupplierName(supplierName);
        spu.setWarehouse(materialBuyPriceVo.getWarehouse());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsSupplierMapper.insert(spu);
        return spu;
    }

}
